using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NapazEngine;
using Napaz.Utilities.Data;
using Napaz.Utilities.Graphics;
using Napaz.Utilities.Controls;

namespace Napaz
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        Camera camera;
        ControlsManager controlsManager;
        
        Texture2D texture;
        SpriteAnimation animation;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";

            Window.Title = Defaults.GAME_NAME;
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Global.Settings = new Settings();
            Global.Settings.SetClientVariableValue("printLog", true);

            int windowWidth = int.Parse(Global.Settings.GetClientSetting("windowWidth").Value.ToString());
            int windowHeight = int.Parse(Global.Settings.GetClientSetting("windowHeight").Value.ToString());

            graphics.PreferredBackBufferWidth = windowWidth;
            graphics.PreferredBackBufferHeight = windowHeight;
            graphics.ApplyChanges();
            
            // create our 2D camera
            camera = new Camera(this);
            camera.WindowSize = new Vector2(windowWidth, windowHeight);
            camera.Position = new Vector2(50, 50);

            controlsManager = new ControlsManager(this);

            // Components must be added
            Components.Add(camera);
            Components.Add(controlsManager);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //sample load a texture
            texture = Content.Load<Texture2D>(@"smiley");

            animation = new SpriteAnimation(texture, texture.Width / 4, texture.Height, 4, 100);
            animation.Play();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            //if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            if(controlsManager.CurrentKeyboardState.IsKeyDown(Keys.Escape))
                this.Exit();

            if (controlsManager.CurrentKeyboardState.IsKeyDown(controlsManager.Controls[PlayerAction.MoveUp]))
                camera.Position = new Vector2(camera.Position.X, camera.Position.Y - 10);
            if (controlsManager.CurrentKeyboardState.IsKeyDown(controlsManager.Controls[PlayerAction.MoveDown]))
                camera.Position = new Vector2(camera.Position.X, camera.Position.Y + 10);
            if (controlsManager.CurrentKeyboardState.IsKeyDown(controlsManager.Controls[PlayerAction.MoveLeft]))
                camera.Position = new Vector2(camera.Position.X - 10, camera.Position.Y);
            if (controlsManager.CurrentKeyboardState.IsKeyDown(controlsManager.Controls[PlayerAction.MoveRight]))
                camera.Position = new Vector2(camera.Position.X + 10, camera.Position.Y);

            animation.Update(gameTime);

            if (!animation.Active)
                animation.Play();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //clear out the graphics device
            GraphicsDevice.Clear(Color.DeepSkyBlue);

            //must always call SpriteBatch.Begin before you draw any graphics
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transformation);

            // draws the texture. Notice the top left corner of the texture 
            // is in the center of the screen
            spriteBatch.Draw(texture, new Vector2(-texture.Width, -texture.Height), Color.White);

            spriteBatch.Draw(animation.Texture, Vector2.Zero, animation.AnimationCellRectangle, Color.White);

            // end the sprite batch
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
